package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.PageBean;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类管理业务层
 * @author Asin
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private SetmealMapper setmealMapper;
    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public R<PageBean> findByPage(int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Category> categories = categoryMapper.findAll();
        if (categories!=null){
            PageInfo<Category> pageInfo = new PageInfo<>(categories);
            return R.success(new PageBean(pageInfo.getList(),pageInfo.getTotal(),page,pageSize));
        }else {
            return R.error("查询失败");
        }

    }

    /**
     * 新增菜品或者套餐
     *
     * @param category
     * @return
     */
    @Override
    public R<String> addCategory(Category category) {


        if (categoryMapper.findCategoryByName(category.getName())!=null){
            return R.error("分类名称已存在,请重新输入");
        }

        return categoryMapper.addCategory(category) > 0 ? R.success("添加成功") :R.error("添加失败");

    }

    /**
     * 更新菜品或者套餐
     *
     * @param category
     * @return
     */
    @Override
    public R<String> updateCategory(Category category) {

        if (categoryMapper.findCategoryByName(category.getName())!=null){
            return R.error("分类名称已存在,请重新输入");
        }
        return categoryMapper.updateCategory(category) > 0 ? R.success("更新成功") :R.error("更新失败");
    }

    /**
     * 根据id删除菜品
     *
     * @param id
     * @return
     */
    @Override
    public R<String> deleteCategoryById(Long id) {
        //删除菜品分类前需要查询是否有套餐或者菜品关联此分类
        if(dishMapper.findDishByCategoryId(id)!=null||setmealMapper.findSetmealByCategoryId(id)!=null){
//            return R.error("该类别关联了菜品或套餐！");
            throw new CustomException("该类别关联了菜品或套餐");
        }else {
            return categoryMapper.deleteCategoryById(id)>0 ? R.success("删除成功") : R.error("删除失败");
        }

    }

    /**
     * 查询分类名称集合
     *
     * @param type
     * @return
     */
    @Override
    public R<List<Category>> findCategoryByType(Integer type) {
        List<Category> categoryByType = categoryMapper.findCategoryByType(type);
        return categoryByType !=null ? R.success(categoryByType) : R.error("查询失败");
    }


}
